import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from "@angular/common/http";

import { Observable, of } from "rxjs";
import { tap } from 'rxjs/operators';
@Injectable({
  providedIn: 'root'
})
export class SubmaterialManageService {

  private baseUrl: string = "/api/material"
  constructor(
    private http: HttpClient
  ) { }
  apiList(data): Observable<any> {
    let _dataJson = {
      params: {
        ...data
      }
    }
    return this.http.get(this.baseUrl, _dataJson).pipe();
  }
  //修改
  private editUrl = '/api/material'
  edit(data): Observable<any> {
    let _dataJson = data
    return this.http.put(this.editUrl, _dataJson).pipe();
  }
  //新加
  private addUrl = '/api/material'
  add(data): Observable<any> {
    let _dataJson = data
    var w = this.http.post(this.editUrl, _dataJson).pipe();
    console.log(w);
    return this.http.post(this.editUrl, _dataJson).pipe();
  }

  private delUrl = "/api/material";
  apiDelete(data): Observable<any> {
    let _dataJson = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
      }),
      body: data
    }
    return this.http.delete(this.delUrl, _dataJson).pipe();
  }
  private modelUrl = "/api/material/detail";
  //获取明细
  GetModel(data): Observable<any> {
    let _dataJson = {
      params: {
        ...data
      }
    }
    return this.http.get(this.modelUrl, _dataJson).pipe();
  }
  private clTypeUrl = '/api/dictDetail'
  async getclTypeList(data): Promise<Node> {
    let dataJson = {
      dictName: data
    }
    return new Promise<Node>((resolve, reject) => {
      this.http.get(this.clTypeUrl, { params: dataJson }).subscribe((res: any) => {
        resolve(res);
      }, err => {
        reject(err)
      })
    })
  }

  //#region 规格
  //新加
  private addSpecUrl = '/api/materialSpec'
  addSpec(data): Observable<any> {
    let _dataJson = data
    return this.http.post(this.addSpecUrl, _dataJson).pipe();
  }

  //修改
  private editSpecUrl = '/api/materialSpec'
  editSpec(data): Observable<any> {
    let _dataJson = data
    return this.http.put(this.editSpecUrl, _dataJson).pipe();
  }

  //删除
  private delSpecUrl = '/api/materialSpec'
  delSpec(data): Observable<any> {
    let _dataJson = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
      }),
      body: data
    }
    return this.http.delete(this.delSpecUrl, _dataJson).pipe();
  }


  //获取库房库区
  private WareHouseUrl = '/api/warehouse'
  GetWareHouse(data):Observable<any>
  {
   // let _dataJson = data
    let _dataJson = {
      params: {
        ...data
      }
    } 
    return this.http.get(this.WareHouseUrl, _dataJson).pipe();
  }

//获取规格列表
  private speListUrl="/api/materialSpec";
  GetSpeList(data):Observable<any>
  {
   // let _dataJson = data;
   let _dataJson = {
    params: {
      ...data
    }
  } 
   return this.http.get(this.speListUrl, _dataJson).pipe(
      tap(( data )=>{
        console.log(data);
      })
    );
  }
  //#endregion

  private storageListByIdUrl="/api/stock";
  GetStorageListById(data):Observable<any>
  {
   let _dataJson = {
    params: {
      ...data
    }
  } 
   return this.http.get(this.storageListByIdUrl, _dataJson).pipe();
  }

  // 获取全部物料
  private getAllMaterialUrl = '/api/material/all';
  getAllMaterial(data): Observable<any>{
    let _dataJson = {
      ...data
    }
    return this.http.get(this.getAllMaterialUrl,{params:_dataJson}).pipe()
  }

  private erpDataUrl="/api/material/get-erp-Data";
  erpData(): Observable<any> {
    let _dataJson={};
    return this.http.get(this.erpDataUrl,_dataJson).pipe();
  }
}
